package com.tj.jinzao.common;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

/**
 * SharedPreference 保存变量
 * Created by wjy on 2015/11/2.
 */
public class Settings {

    public final static String KEY_PREF_ENABLE_TRACING = "pref_enable_tracing";//打印开关
    public final static String KEY_PREF_ENABLE_DEBUG = "pref_enable_debug";//调试模式
    public final static String KEY_PREF_TONGUE_ID = "pref_tongue_id";//上传舌头的id
    public final static String KEY_PREF_FACE_ID = "pref_face_id";//上传脸部的id
    public final static String KEY_PREF_FAMILY_ID = "pref_family_id";//上传的家庭id
    public final static String KEY_PREF_ANSWER = "pref_answer";//智能诊断的答案
    public final static String KEY_PREF_WIFI_AUTO_PLAY = "wifi_auto_play";//WIFI自动播放视频
    public final static String KEY_PREF_3G_AUTO_PLAY = "3g_auto_play";//3G自动播放视频
    public final static String KEY_PREF_PHONE_CAMERA_PIC = "phone_camera_pic";//手机多次拍照
    public final static String KEY_PREF_LIST_CURRENT_PAGE = "prefs_topic_list_current_page_";//各列表当前页
    public final static String KEY_PREF_LIST_LAST_REQUEST = "prefs_topic_list_last_request_";//各列表上次请求时间

    public final static String KEY_PREF_USER_TOKEN = "prefs_user_token";//用户token
    public final static String KEY_PREF_USER_ID = "prefs_user_id";//用户id
    public final static String KEY_PREF_USER = "prefs_user";//用户json
    public final static String KEY_PREF_CITY = "prefs_city";//城市
    public final static String KEY_PREF_LAT = "prefs_lat";//经纬度
    public final static String KEY_PREF_LNG = "prefs_lng";
    private static SharedPreferences sUserPref;
    private static SharedPreferences sTimePref;

    public static SharedPreferences getSharedPreferences(Context context) {
        if (sUserPref == null) {
            sUserPref = context.getSharedPreferences("user_data", Context.MODE_PRIVATE);
        }
        return sUserPref;
    }

    public static SharedPreferences getTimeSharedPreferences(Context context) {
        if (sTimePref == null) {
            sTimePref = context.getSharedPreferences("request_time", Context.MODE_PRIVATE);
        }
        return sTimePref;
    }

    public static String getString(Context context, String key, String defaultValue) {
        return getSharedPreferences(context).getString(key, defaultValue);
    }

    public static void setString(Context context, String key, String value) {
        getSharedPreferences(context).edit().putString(key, value).commit();
    }

    public static boolean getBoolean(Context context, String key, boolean defaultValue) {
        return getSharedPreferences(context).getBoolean(key, defaultValue);
    }

    public static void setBoolean(Context context, String key, boolean value) {
        getSharedPreferences(context).edit().putBoolean(key, value).commit();
    }

    public static int getInt(Context context, String key, int defaultValue) {
        return getSharedPreferences(context).getInt(key, defaultValue);
    }

    public static void setInt(Context context, String key, int value) {
        getSharedPreferences(context).edit().putInt(key, value).commit();
    }

    public static float getFloat(Context context, String key, float defaultValue) {
        return getSharedPreferences(context).getFloat(key, defaultValue);
    }

    public static void setFloat(Context context, String key, float value) {
        getSharedPreferences(context).edit().putFloat(key, value).commit();
    }

    public static long getLong(Context context, String key, long defaultValue) {
        return getSharedPreferences(context).getLong(key, defaultValue);
    }

    public static void setLong(Context context, String key, long value) {
        getSharedPreferences(context).edit().putLong(key, value).commit();
    }

    public static long getLongTime(Context context, String key, long defaultValue) {
        return getTimeSharedPreferences(context).getLong(key, defaultValue);
    }

    public static void setLongTime(Context context, String key, long value) {
        getTimeSharedPreferences(context).edit().putLong(key, value).commit();
    }

    public static void clearUserRequestTime(Context context) {
        Editor editor = getTimeSharedPreferences(context).edit();
        editor.clear().commit();
    }

    public static void saveCity(Context context, String city, double lat, double lng) {
        Editor editor = getSharedPreferences(context).edit();
        editor.putString(KEY_PREF_CITY, city);
        editor.putString(KEY_PREF_LAT, String.valueOf(lat));
        editor.putString(KEY_PREF_LNG, String.valueOf(lng));
        editor.clear().commit();
    }

    /**
     * 缓存清空
     *
     * @param context
     */
    public static void clearUserData(Context context) {
        if (context != null) {
            try {
                //清空用户操作数据
                clearUserRequestTime(context);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}